package com.socialsoftware.model;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

/**
 * 好友请求数据访问对象
 * 用于操作好友请求数据库表
 */
@Dao
public interface FriendRequestDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(FriendRequest friendRequest);
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<FriendRequest> friendRequests);
    
    @Update
    void update(FriendRequest friendRequest);
    
    @Delete
    void delete(FriendRequest friendRequest);
    
    @Query("DELETE FROM friend_requests WHERE id = :id")
    void deleteById(Long id);
    
    @Query("SELECT * FROM friend_requests WHERE id = :id")
    FriendRequest getById(Long id);
    
    @Query("SELECT * FROM friend_requests WHERE addresseeId = :userId AND status = 'PENDING'")
    LiveData<List<FriendRequest>> getPendingRequestsForUser(Long userId);
    
    @Query("SELECT * FROM friend_requests WHERE addresseeId = :userId AND status = 'PENDING'")
    List<FriendRequest> getPendingRequestsForUserSync(Long userId);
    
    @Query("SELECT * FROM friend_requests WHERE addresseeId = :userId")
    LiveData<List<FriendRequest>> getAllRequestsForUser(Long userId);
    
    @Query("SELECT COUNT(*) FROM friend_requests WHERE addresseeId = :userId AND status = 'PENDING'")
    LiveData<Integer> getPendingRequestsCount(Long userId);
    
    @Query("SELECT COUNT(*) FROM friend_requests WHERE addresseeId = :userId AND status = 'PENDING'")
    int getPendingRequestsCountSync(Long userId);
    
    @Query("UPDATE friend_requests SET status = :status, updatedAt = :updatedAt WHERE id = :id")
    void updateStatus(Long id, String status, long updatedAt);
    
    @Query("DELETE FROM friend_requests")
    void deleteAll();
    
    /**
     * 删除指定用户的所有待处理好友请求
     * @param userId 用户ID
     */
    @Query("DELETE FROM friend_requests WHERE addresseeId = :userId AND status = 'PENDING'")
    void deleteAllPendingRequestsForUser(Long userId);
} 